<?php

namespace App\Models;

use Exception;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

/*总数字资源model*/

class ResourceTotal extends BaseModel
{
    use HasFactory;

    const CREATED_AT = 'create_time';
    const UPDATED_AT = 'change_time';


    protected $table = 'resource_total';


    /*关联活动类型*/
    public function conBigType()
    {
        return $this->hasOne(ResourceType::class, 'id', 'type_id');
    }


    /**
     * 检索 mysql 数据 （全数据检索）
     * @param keywords 检索条件
     * @param big_type_id 系统大类
     * @param type_id 系统小类 数组形式
     * @param page int 当前页
     * @param limit int 分页大小
     * @param is_play int 是否发布 1 已发布 2 未发布
     */
    public function lists($keywords, $big_type_id = null, $type_id = null, $is_play = null, $limit = 10)
    {
        $res = $this->select([
            // 'id',
            'guid',
            'title',
            'author',
            'isbn',
            'press',
            'pre_time',
            'img',
            'intro',
            'create_time',
            'big_type_id',
            'type_id',
        ])
            ->with('conType', function ($query) {
                $query->select('id', 'type_name')->where('is_del', 1);
            })
            ->where(function ($query) use ($keywords) {
                if ($keywords) {
                    $query->where('title', 'like', "%$keywords%");
                }
            })->where(function ($query) use ($big_type_id, $type_id, $is_play) {
                //系统大类
                if (!empty($big_type_id)) {
                    $query->where('big_type_id', $big_type_id);
                }
                //小类
                if (!empty($type_id)) {
                    $type_id = !is_array($type_id) ? explode(',', $type_id) : $type_id;
                    $query->whereIn('type_id', $type_id);
                }
                if ($is_play) {
                    $query->where('is_play', $is_play);
                }
            })
            ->where('is_del', 1)
            // ->orderBy('is_recom')
            ->orderByDesc('id')
            ->paginate($limit)
            ->toArray();

        return $res;
    }

    /**
     * 根据类型获取数据
     */
    public function getResourceListByBigTypeId($big_type_id = null, $limit = 10)
    {
        return $this->select([
            // 'id',
            'guid',
            'title',
            'author',
            'isbn',
            'press',
            'pre_time',
            'img',
            'intro',
            'create_time',
            'big_type_id',
            'type_id',
        ])->where('is_del', 1)
            ->where('big_type_id', $big_type_id)
            ->limit($limit)
            ->get()
            ->toArray();
    }



    /**
     * 获取书籍详情
     * @param id  书籍id
     * @param guid  书籍guid  和 书籍id 2选一
     */
    public function detail($id = null, $guid = null, $is_play = null)
    {
        if (empty($id) && empty($guid)) {
            return false;
        }
        $res = $this->select([
            'id',
            'type_id',
            'big_type_id',
            'guid',
            'title',
            'author',
            'isbn',
            'press',
            'pre_time',
            'img',
            'intro',
            'create_time',
        ])->where(function ($query) use ($id, $guid, $is_play) {
            if ($id) {
                $query->where('id', $id);
            } else {
                $query->where('guid', $guid);
            }
            if ($is_play) {
                $query->where('is_play', $is_play);
            }
        })->where('is_del', 1)
            ->first();
        return $res;
    }

    /**
     *添加浏览量
     * @param id  书籍id
     * @param resource_business_id  资源商id
     */
    public function addBrowseNum($resource_business_id, $id)
    {
        if (empty($id) && empty($guid)) {
            return false;
        }
        $model = $this->getResourceTotalModel($resource_business_id);
        if (empty($model)) return false;
        $res = $model->where('id', $id)->where('is_del', 1)->increment('browse_num');
        if ($res && empty($guid)) {
            $guid = $model->where('id', $id)->where('is_del', 1)->value('guid');
        }
        if ($res && $guid) {
            ResourceTotal::where('guid', $guid)->where('is_del', 1)->increment('browse_num'); //更新总表数据id
        }

        return $res;
    }


    public function addData($data)
    {
        $res = $this->where('guid', $data->guid)->first();
        if (empty($res)) {
            //修改
            $res = new self();
        }
        $res->big_type_id = $data->big_type_id;
        $res->type_id = $data->type_id;
        $res->guid = $data->guid;
        $res->title = $data->title;
        $res->author = $data->author;
        $res->isbn = $data->isbn;
        $res->press = $data->press;
        $res->pre_time = $data->pre_time;
        $res->img = $data->img;
        $res->intro = $data->intro;
        $res->save();
        return true;
    }

    public function deleteData($guid)
    {
        $res = $this->where('guid', $guid)->where('is_del', 1)->first();
        if (empty($res)) {
            return true;
        }
        $res->is_del = 2;
        $res->save();
        return true;
    }


    /**
     *获取推荐资源
     */
    public function getRecomResourceLists($big_type_id, $limit = 9)
    {
        $res = $this->select([
            'id',
            'guid',
            'resource_business_id',
            'title',
            'author',
            'isbn',
            'press',
            'pre_time',
            'img',
            'intro',
            'create_time',
            'big_type_id',
            'type_id',
        ])
            ->with('conType', function ($query) {
                $query->select('id', 'type_name')->where('is_del', 1);
            })->where(function ($query) use ($big_type_id) {
                //系统大类
                if (!empty($big_type_id)) {
                    $query->where('big_type_id', $big_type_id);
                }
            })
            ->where('is_del', 1)
            ->inRandomOrder()
            ->limit($limit)
            ->get()
            ->toArray();

        return $res;
    }

    /**
     *获取资源模型
     */
    public function getResourceTotalModel($big_type_id)
    {
        switch ($big_type_id) {
            case 1:
                $model = new ResourceEbookInfo();
                break;
            case 2:
                $model = new ResourceAudioInfo();
                break;
            case 3:
                $model = new ResourceVideoInfo();
                break;
            default:
                $model = false;
        }
        return $model;
    }


    /**
     *获取资源章节模型
     */
    public function getResourceChapterInfoModel($big_type_id)
    {
        switch ($big_type_id) {
            case 1:
                $model = new ResourceEbookChapterInfo();
                break;
            case 2:
                $model = new ResourceAudioChapterInfo();
                break;
            case 3:
                $model = new ResourceVideoGatherInfo();
                break;
            default:
                $model = false;
        }
        return $model;
    }



    /**
     * 获取所有下架资源id
     * @param big_type_id id  系统大类 1 电子书  2 有声书（音频） 3 视频  空表示全部
     * @param page 页数
     * @param limit 每页显示条数
     */
    public function getDownResourceId($big_type_id, $limit = 10)
    {
        $res = $this->select('guid')->where(function ($query) use ($big_type_id) {
            //系统大类
            if (!empty($big_type_id)) {
                $query->where('big_type_id', $big_type_id);
            }
        })->where(function ($query) {
            //系统大类
            $query->where('is_del', 2)->Orwhere('is_play', 2);
        })
            ->paginate($limit)
            ->toArray();
        if ($res['data']) {
            return array_column($res['data'], 'guid');
        }
        return [];
    }
}
